//
//  M4BaseViewController+UITableViewExtension.swift
//  M4
//
//  Created by YuChuang on 2024/8/12.
//

import Foundation
import UIKit
import MZRefresh

extension M4BaseViewController : UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return M4TableSources.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellData = M4TableSources[indexPath.row]
        if cellData.M4XIB == true {
            tableView.register(UINib(nibName: cellData.M4CellName, bundle: nil), forCellReuseIdentifier: cellData.M4CellName)
        } else {
            let a = "M4.\(cellData.M4CellName)"
            tableView.register(NSClassFromString(a), forCellReuseIdentifier: cellData.M4CellName)
        }
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellData.M4CellName, for: indexPath) as? M4BaseTableViewCell else { return UITableViewCell() }
        cell.indexPath = indexPath
        cell.data = cellData.M4Data1
        cell.data2 = cellData.M4Data2
        cell.data3 = cellData.M4Data3
        cell.M4CellActionDelegate = self
        return cell
    }
    
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        let item = M4TableSources[indexPath.row]
//        if item.M4Height == UITableView.automaticDimension {
//            return UITableView.automaticDimension
//        }
//        return item.M4Height
//    }
//    
//    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
//        let item = M4TableSources[indexPath.row]
//        if item.M4Height == UITableView.automaticDimension {
//            return 160
//        }
//        return 0
//    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView.init(frame: .zero)
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView.init(frame: .zero)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    public func M4tableStyle() -> UITableView.Style {
        return .plain
    }
    
}

extension M4BaseViewController {
    
    func M4addRefreshToTaleView(refreshBlock:@escaping()->Void) {
        M4TableView.setRefreshHeader(MZRefreshNormalHeader(
            type: .ballClipRotateMultiple,
            color: .systemPink,
            beginRefresh: {
                refreshBlock()
            }))
    }
    
    func M4addFooterToTableView(refreshBlock:@escaping()->Void) {
        M4TableView.setRefreshFooter(MZRefreshNormalFooter(
            type: .lineScale,
            color: .brown,
            beginRefresh: {
                refreshBlock()
            }))
    }
    
    func M4endRefresh() {
        M4TableView.stopHeaderRefreshing()
        M4TableView.stopFooterRefreshing()
    }
    
}



struct M4TableViewCellData {
    var M4CellName: String = ""
    var M4Data1: Any
    var M4Data2: Any
    var M4Data3: Any
    var M4XIB: Bool = false
    var M4Height: CGFloat = UITableView.automaticDimension
    
    init(_ cellName: String, XIB: Bool = false, _ data1: Any = "", _ data2: Any = "", _ data3: Any = "", height: CGFloat = UITableView.automaticDimension) {
        self.M4CellName = cellName
        self.M4Data1 = data1
        self.M4Data2 = data2
        self.M4Data3 = data3
        self.M4XIB = XIB
        self.M4Height = height
    }
}

class M4BaseTableViewCell: UITableViewCell {
    
    weak var M4CellActionDelegate: (M4BaseTableViewCellProtocol)?
    
    @IBOutlet weak var cardView: UIView?
    
    var data: Any? {
        didSet {
            M4UpdateDataAndUI()
        }
    }
    
    var data2: Any? {
        didSet {
            M4SecondDataRefresh()
        }
    }
    
    var data3: Any? {
        didSet {
            M4ThirdDataRefresh()
        }
    }
    
    var indexPath: IndexPath?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setViewsLayout()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.selectionStyle = .none
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        //        fatalError("init(coder:) has not been implemented")
    }
    
    func M4UpdateDataAndUI() {
        
    }
    
    func M4SecondDataRefresh() {
        
    }
    
    func M4ThirdDataRefresh() {
        
    }
    
    func setViewsLayout() {
        cardView?.layer.cornerRadius = 8
        cardView?.layer.masksToBounds = true
        cardView?.backgroundColor = .white
    }
    
    override var frame: CGRect {
        didSet {
            M4resetFrame()
        }
    }
    
    func M4CellLeftRightEdge() -> CGFloat { return 15 }
    func M4CellTopEdge() -> CGFloat { return 0 }
    
    func M4resetFrame() {
        var newFrame = frame
        newFrame.origin.x += M4CellLeftRightEdge()
        newFrame.origin.y += M4CellTopEdge()
        newFrame.size.width = M4ScreenWidth - M4CellLeftRightEdge() * 2
        super.frame = newFrame
    }
}

class M4IDAction {
    var M4ActionType: Int = 0
    var M4ActionData: Any?
    
    init(actionType: Int, actionData: Any? = nil) {
        self.M4ActionType = actionType
        self.M4ActionData = actionData
    }
}

extension UIViewController {
    func M4showTopViewController() -> UIViewController {
        if let firstVC = self.presentedViewController {
            return firstVC.M4showTopViewController()
        } else if let nav = self as? UINavigationController {
            return nav.visibleViewController?.M4showTopViewController() ?? nav
        } else if let tab = self as? UITabBarController {
            return tab.selectedViewController?.M4showTopViewController() ?? tab
        }
        return self
    }
}

